/*
 * Created by JFormDesigner on Fri Jun 16 16:45:54 CST 2023
 */

package fm.supplier;

import dao.Dao_supplier;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;

/**
 * @author YangC
 */
public class AddSupplier extends JInternalFrame {
    public AddSupplier() {
        initComponents();
    }

    private void cancel(ActionEvent e) {
        doDefaultCloseAction();
    }

    private void add(ActionEvent e) {
        List<String> list = Dao_supplier.getSupplierID();

        if(supplierIDTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入供应商编号！");
            return;
        }
        for (String s : list) {
            if (s.equals(supplierIDTextField.getText())) {
                JOptionPane.showMessageDialog(null, "供应商ID已存在，请重新设置供应商编号！");
                return;
            }
        }



        if(supplierIDTextField.getText().length() != 4)
        {
            JOptionPane.showMessageDialog(null,"供应商编号应为四位！");
            return;
        }

        if(sNameBextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入供应商名称！");
            return;
        }

        if(peopleTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入联系人！");
            return;
        }

        if(phoneTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入联系电话！");
            return;
        }

        if(phoneTextField.getText().trim().length() > 11)
        {
            JOptionPane.showMessageDialog(null,"联系电话输入有误！");
            return;
        }

        if(addressTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入地址！");
            return;
        }

        String s_id = supplierIDTextField.getText().trim();
        String s_name = sNameBextField.getText().trim();
        String s_phone = phoneTextField.getText().trim();
        String s_people = peopleTextField.getText().trim();
        String address = addressTextField.getText().trim();

        int i = Dao_supplier.addSu(s_id, s_name, s_phone, s_people,address);

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"添加成功！");
            doDefaultCloseAction();
        }

    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        addButton = new JButton();
        cancelButton = new JButton();
        panel3 = new JPanel();
        label1 = new JLabel();
        supplierIDTextField = new JTextField();
        label2 = new JLabel();
        sNameBextField = new JTextField();
        label3 = new JLabel();
        peopleTextField = new JTextField();
        label4 = new JLabel();
        addressTextField = new JTextField();
        label5 = new JLabel();
        phoneTextField = new JTextField();
        label6 = new JLabel();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("\u4f9b\u5e94\u5546\u6dfb\u52a0");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(356, 40));
            panel1.setLayout(null);

            //---- addButton ----
            addButton.setText("\u6dfb\u52a0");
            addButton.addActionListener(e -> add(e));
            panel1.add(addButton);
            addButton.setBounds(new Rectangle(new Point(181, 6), addButton.getPreferredSize()));

            //---- cancelButton ----
            cancelButton.setText("\u53d6\u6d88");
            cancelButton.addActionListener(e -> cancel(e));
            panel1.add(cancelButton);
            cancelButton.setBounds(new Rectangle(new Point(335, 6), cancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 334, 558, panel1.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setLayout(null);

            //---- label1 ----
            label1.setText("\u4f9b\u5e94\u5546\u7f16\u53f7\uff1a");
            panel3.add(label1);
            label1.setBounds(46, 20, label1.getPreferredSize().width, 19);
            panel3.add(supplierIDTextField);
            supplierIDTextField.setBounds(122, 15, 101, supplierIDTextField.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u4f9b\u5e94\u5546\uff1a");
            panel3.add(label2);
            label2.setBounds(300, 21, label2.getPreferredSize().width, 19);
            panel3.add(sNameBextField);
            sNameBextField.setBounds(355, 17, 105, sNameBextField.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u8054\u7cfb\u4eba\uff1a");
            panel3.add(label3);
            label3.setBounds(69, 65, label3.getPreferredSize().width, 19);
            panel3.add(peopleTextField);
            peopleTextField.setBounds(122, 60, 100, peopleTextField.getPreferredSize().height);

            //---- label4 ----
            label4.setText("\u5730\u5740\uff1a");
            panel3.add(label4);
            label4.setBounds(311, 65, label4.getPreferredSize().width, 19);
            panel3.add(addressTextField);
            addressTextField.setBounds(356, 59, 103, addressTextField.getPreferredSize().height);

            //---- label5 ----
            label5.setText("\u8054\u7cfb\u7535\u8bdd\uff1a");
            panel3.add(label5);
            label5.setBounds(182, 102, label5.getPreferredSize().width, 21);
            panel3.add(phoneTextField);
            phoneTextField.setBounds(243, 98, 120, phoneTextField.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 200, 558, 134);

        //---- label6 ----
        label6.setIcon(new ImageIcon(getClass().getResource("/ref/supplier.png")));
        contentPane.add(label6);
        label6.setBounds(-1, 0, 590, 215);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton addButton;
    private JButton cancelButton;
    private JPanel panel3;
    private JLabel label1;
    private JTextField supplierIDTextField;
    private JLabel label2;
    private JTextField sNameBextField;
    private JLabel label3;
    private JTextField peopleTextField;
    private JLabel label4;
    private JTextField addressTextField;
    private JLabel label5;
    private JTextField phoneTextField;
    private JLabel label6;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
